package cn.liyunqi.ttv.start.controller;

import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.core.util.StringUtil;
import cn.liyunqi.ttv.start.action.TTVActionUtil;
import cn.liyunqi.ttv.start.view.AlertUtil;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.ResourceBundle;

public class ForgetPasswordController extends BaseFXController {

    @FXML
    private TextField mailTxt;

    @FXML
    private TextField mailCodeTxt;

    @FXML
    private TextField passwordTxt;

    @FXML
    private TextField passwordAgainTxt;

    @FXML
    private Button mailCodeBtn;

    private InitAppController initUIController;


    void setInitUIController(InitAppController initUIController) {
        this.initUIController = initUIController;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @FXML
    public void doGetHandle(){
        String mailValue = mailTxt.getText();
        if(StringUtil.isEmptyOrNull(mailValue) || !TTVActionUtil.isEmail(mailValue)){
            AlertUtil.showErrorAlert("请填写有效的邮箱地址");
            return;
        }
        mailValue = mailValue.trim();

        String mailCode = mailCodeTxt.getText();
        if(StringUtil.isEmptyOrNull(mailCode)){
            AlertUtil.showErrorAlert("请填写邮箱验证码");
            return;
        }
        mailCode = mailCode.trim();

        String password = passwordTxt.getText();
        if(password == null || password.length() < 6){
            AlertUtil.showErrorAlert("请填写6位及以上密码");
            return;
        }

        String passwordAgain = passwordAgainTxt.getText();
        if(passwordAgain == null || passwordAgain.length() < 6){
            AlertUtil.showErrorAlert("两次输入密码不一致");
            return;
        }
        if(!password.equals(passwordAgain)){
            AlertUtil.showErrorAlert("两次输入密码不一致");
            return;
        }

        try {
            if(TTVActionUtil.resetPassword(mailValue,mailCode,password)){

                AlertUtil.showInfoAlert("修改成功");
                getDialogStage().close();

            }else{
                AlertUtil.showErrorAlert("修改失败");
            }
        }catch (BizException ex){
            AlertUtil.showErrorAlert(ex.getMessage());
        }
    }

    @FXML
    public void sendMailCodeHandle(){
        String mailValue = mailTxt.getText();
        if(StringUtil.isEmptyOrNull(mailValue) || !TTVActionUtil.isEmail(mailValue)){
            AlertUtil.showErrorAlert("请填写有效的邮箱地址");
            return;
        }
        try {
            if(TTVActionUtil.sendMailCode(mailValue,2)){
                mailCodeBtn.setDisable(true);

                Runnable runnable = new Runnable() {
                    public void run() {
                        for (int i = 30; i > 0 ; i--){

                            int finalI = i;
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    mailCodeBtn.setText("获取("+ finalI +"s)");
                                }
                            });

                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                mailCodeBtn.setText("获取");
                                mailCodeBtn.setDisable(false);
                            }
                        });

                    }
                };
                Thread thread = new Thread(runnable);
                thread.start();

            }else{
                AlertUtil.showErrorAlert("邮箱验证码发送失败");
            }
        }catch (BizException biz){
            AlertUtil.showErrorAlert(biz.getMessage());
        }
    }
}
